package com.cn.crmsystem.service.systemService.impl;

import com.cn.crmsystem.mapper.systemMapper.DeptMapper;
import com.cn.crmsystem.pojo.Department;
import com.cn.crmsystem.pojo.Login;
import com.cn.crmsystem.pojo.ResultJson;
import com.cn.crmsystem.pojo.User;
import com.cn.crmsystem.service.systemService.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptServiceImpl implements DeptService{

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public ResultJson addDept(Department department, Login login) {
        HashMap<Object,Object> map = new HashMap<>();
        map.put("department",department);
        map.put("login",login);
        if (deptMapper.addDept(map) > 0){
            return new ResultJson().getSuccess("新增部门成功");
        }
        return new ResultJson().getFail("新增部门失败");
    }

    @Override
    public ResultJson selectDept(Department department) {
        List<Department> list = deptMapper.selectDept(department);
        if (list != null && list.size() > 0){
            return new ResultJson().getSuccess("查询成功",list);
        }
        return new ResultJson().getFail("查询失败");
    }

    @Override
    public ResultJson deleteDept(Integer id) {
        if (deptMapper.deleteDept(id) > 0){
            return new ResultJson().getSuccess("删除成功");
        }
        return new ResultJson().getFail("删除失败");
    }

    @Override
    public ResultJson updateDept(Department department, Login login) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("department",department);
        map.put("login",login);
        if (deptMapper.updateDept(map) > 0){
            return new ResultJson().getSuccess("修改成功");
        }
        return new ResultJson().getFail("修改失败");
    }

    @Override
    public ResultJson deleteManyDept(Integer[] ids) {
        int result=0;
        for(int i=0;i<ids.length;i++){
            result=deptMapper.deleteDept(ids[i]);
        }
        if(result>0){
            return new ResultJson<>().getSuccess("删除成功");
        }else{
            return new ResultJson<>().getSuccess("删除失败");
        }
    }

}
